package com.moxiao.yfjh.module.maker.controller.app;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.module.blind.dal.dataobject.member.BlindMemberUserDO;
import com.moxiao.yfjh.module.blind.service.member.BlindMemberUserService;
import com.moxiao.yfjh.module.maker.controller.admin.matchMakerStringing.vo.MatchmakerStringingPageReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchMakerStringing.vo.MatchmakerStringingRespVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchMakerStringing.vo.MatchmakerStringingUpdateReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchmakerInfo.vo.MatchmakerInfoRespVO;
import com.moxiao.yfjh.module.maker.controller.admin.memberStringing.vo.MatchmakerMemberStringingExportReqVO;
import com.moxiao.yfjh.module.maker.controller.app.vo.MyMakerStringingRespVO;
import com.moxiao.yfjh.module.maker.convert.matchMakerStringing.MatchmakerStringingConvert;
import com.moxiao.yfjh.module.maker.convert.matchmakerInfo.MatchmakerInfoConvert;
import com.moxiao.yfjh.module.maker.dal.dataobject.matchMakerStringing.MatchmakerStringingDO;
import com.moxiao.yfjh.module.maker.dal.dataobject.matchmakerInfo.MatchmakerInfoDO;
import com.moxiao.yfjh.module.maker.dal.dataobject.memberStringing.MatchmakerMemberStringingDO;
import com.moxiao.yfjh.module.maker.service.matchMakerStringing.MatchmakerStringingService;
import com.moxiao.yfjh.module.maker.service.matchmakerInfo.MatchmakerInfoService;
import com.moxiao.yfjh.module.maker.service.memberStringing.MatchmakerMemberStringingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * @version 1.0
 * @Author zhk
 * @Date 2024/2/29 11:01
 **/
@Tag(name = "APP - 相亲红娘牵线")
@RestController
@RequestMapping("/maker/matchmaker-stringing")
@Validated
public class AppMakerStringingController {

    @Resource
    private MatchmakerMemberStringingService matchmakerMemberStringingService;
    @Resource
    private MatchmakerInfoService matchmakerInfoService;

    @Resource
    private MatchmakerStringingService matchmakerStringingService;

    @Resource
    private BlindMemberUserService blindMemberUserService;

    @GetMapping("/getMyMatchmaker")
    @Operation(summary = "获得我的红娘信息")
    public CommonResult<MatchmakerInfoRespVO> getMyMatchmaker() {
        Long loginUserId = getLoginUserId();
        MatchmakerMemberStringingExportReqVO matchMaker = new MatchmakerMemberStringingExportReqVO();
        matchMaker.setMemberId(loginUserId);
        List<MatchmakerMemberStringingDO> list = matchmakerMemberStringingService.getMatchmakerMemberStringingList(matchMaker);
        if (CollectionUtils.isNotEmpty(list)){
            Long matchmakerId = list.get(0).getMatchmakerId();
            MatchmakerInfoDO matchmakerInfo = matchmakerInfoService.getMatchmakerInfo(matchmakerId);
            return success(MatchmakerInfoConvert.INSTANCE.convert(matchmakerInfo));
        }
        return success(null);
    }


    @GetMapping("/getMyLeadTimes")
    @Operation(summary = "获得我的牵线次数")
    public CommonResult<MyMakerStringingRespVO> getMyLeadTimes() {
        MyMakerStringingRespVO myMakerStringingRespVO = new MyMakerStringingRespVO();
        //牵线成功记录
        MatchmakerStringingPageReqVO pageReqVO = new MatchmakerStringingPageReqVO();
        pageReqVO.setActiveId(getLoginUserId());
        PageResult<MatchmakerStringingDO> pageResult = matchmakerStringingService.getMatchmakerStringingPage(pageReqVO);
        //牵线总次数
        MatchmakerMemberStringingExportReqVO matchMaker = new MatchmakerMemberStringingExportReqVO();
        matchMaker.setMemberId(getLoginUserId());
        List<MatchmakerMemberStringingDO> list = matchmakerMemberStringingService.getMatchmakerMemberStringingList(matchMaker);
        if (CollectionUtils.isNotEmpty(list)){
            Integer stringingNum = list.get(0).getStringingNum();
            myMakerStringingRespVO.setStringingNum(Long.valueOf(stringingNum));
            myMakerStringingRespVO.setRemainingNum(stringingNum - pageResult.getTotal());
            return success(myMakerStringingRespVO);
        }
        myMakerStringingRespVO.setStringingNum(0L);
        myMakerStringingRespVO.setRemainingNum(0L);
        return success(myMakerStringingRespVO);

    }


    @GetMapping("/getMyLeadRecord")
    @Operation(summary = "获得我的牵线记录")
    public CommonResult<PageResult<MatchmakerStringingRespVO>> getMyLeadRecord() {
        MatchmakerStringingPageReqVO pageReqVO = new MatchmakerStringingPageReqVO();
        pageReqVO.setActiveId(getLoginUserId());
        PageResult<MatchmakerStringingDO> matchmakerStringingPage = matchmakerStringingService.getMatchmakerStringingPage(pageReqVO);
        PageResult<MatchmakerStringingRespVO> matchmakerStringingRespVOPageResult = MatchmakerStringingConvert.INSTANCE.convertPage(matchmakerStringingPage);
        if (Objects.nonNull(matchmakerStringingRespVOPageResult)){
            if (CollectionUtils.isNotEmpty(matchmakerStringingRespVOPageResult.getList())){
                List<MatchmakerStringingRespVO> matchmakerStringingRespVOS = matchmakerStringingRespVOPageResult.getList();
                if (CollectionUtils.isNotEmpty(matchmakerStringingRespVOS)){
                    matchmakerStringingRespVOS.forEach(matchmakerStringingRespVO -> {
                        Long activeId = matchmakerStringingRespVO.getActiveId();
                        Long manId = matchmakerStringingRespVO.getManId();
                        Long womanId = matchmakerStringingRespVO.getWomanId();
                        if (Objects.equals(activeId,manId)){
                            BlindMemberUserDO memberUser = blindMemberUserService.getMemberUser(womanId);
                            if (Objects.nonNull(memberUser)) {
                                matchmakerStringingRespVO.setAvatar(memberUser.getAvatar());
                                matchmakerStringingRespVO.setWomanName(memberUser.getNickname());
                            }
                        }else {
                            BlindMemberUserDO memberUser = blindMemberUserService.getMemberUser(manId);
                            if (Objects.nonNull(memberUser)) {
                                matchmakerStringingRespVO.setAvatar(memberUser.getAvatar());
                                matchmakerStringingRespVO.setManName(memberUser.getNickname());
                            }
                        }
                    });
                }
           }
        }
        return success(matchmakerStringingRespVOPageResult);
    }

    @PutMapping("/agreeToStrings")
    @Operation(summary = "更新相亲红娘牵线记录")
    public CommonResult<Boolean> agreeToStrings(@Valid @RequestBody MatchmakerStringingUpdateReqVO updateReqVO) {
        matchmakerStringingService.updateMatchmakerStringing(updateReqVO);
        return success(true);
    }



}
